package laboratoryQ.flatness;

import java.awt.Graphics;
import java.util.List;

public class FlatnessUtils {
	public static void doTransform(
			double[] fromX,
			double[] fromY,
			double[] toX,
			double[] toY,
			ICoordinateTransformer transformer,
			Graphics g
	) {
		int n = fromX.length;
		doTransform(
				fromX,
				fromY,
				toX,
				toY,
				transformer,
				g,
				n
		);
	}
	
	public static void doTransform(
			double[] fromX,
			double[] fromY,
			double[] toX,
			double[] toY,
			ICoordinateTransformer transformer,
			Graphics g,
			int n
	) {
		System.arraycopy(fromX, 0, toX, 0, n);
		System.arraycopy(fromY, 0, toY, 0, n);
		transformer.transform(toX, toY, g);
	}
	
	public static RangeInfo getDiagramsRange(List<DiagramData> diagramList) {
		RangeInfo rangeInfo = new RangeInfo();
		for (DiagramData dia : diagramList) {
			RangeInfo ri = FlatnessUtils.getDiagramsRange(dia);
			FlatnessUtils.relaxRangeInfo(rangeInfo, ri);
		}
		return rangeInfo;
	}
	
	public static RangeInfo getDiagramsRange(
			DiagramData diagram
	) {
		RangeInfo rangeInfo = new RangeInfo(
				Double.MAX_VALUE,
				-Double.MAX_VALUE,
				Double.MAX_VALUE,
				-Double.MAX_VALUE		
		);
		
		int n = diagram.x.length;
		for (int i = 0; i < n; i++) {
			rangeInfo.minX = Math.min(rangeInfo.minX, diagram.x[i]);
			rangeInfo.minY = Math.min(rangeInfo.minY, diagram.y[i]);
			rangeInfo.maxX = Math.max(rangeInfo.maxX, diagram.x[i]);
			rangeInfo.maxY = Math.max(rangeInfo.maxY, diagram.y[i]);
		}
		
		return rangeInfo;
	}
	
	public static void relaxRangeInfo(RangeInfo bigInfo, RangeInfo currentInfo) {
		bigInfo.minX = Math.min(bigInfo.minX, currentInfo.minX);
		bigInfo.maxX = Math.max(bigInfo.maxX, currentInfo.maxX);
		bigInfo.minY = Math.min(bigInfo.minY, currentInfo.minY);
		bigInfo.maxY = Math.max(bigInfo.maxY, currentInfo.maxY);
	}
	
	public static void relaxRangeInfo(RangeInfo bigInfo, Point p) {
		bigInfo.minX = Math.min(bigInfo.minX, p.x);
		bigInfo.maxX = Math.max(bigInfo.maxX, p.x);
		bigInfo.minY = Math.min(bigInfo.minY, p.y);
		bigInfo.maxY = Math.max(bigInfo.maxY, p.y);
	}
	
	public static ICoordinateTransformer generateEasyUITransformer(int boardSize, RangeInfo rangeInfo, boolean commonScaleMult) {
		BoardTransformer boardTransformer = new BoardTransformer(
		boardSize,
		rangeInfo,
		commonScaleMult
		);
		RevertYTransformer revertYTransformer = new RevertYTransformer();
		return new MultiTransformer(boardTransformer, revertYTransformer);
	}
}
